Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++20 Modules Support #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

tsarn
Copy link

@tsarn tsarn commented Apr 27, 2024

Hi! Thanks for the project :) I've made a PR adding support for C++20 modules. In this implementation, module's source is #ifdefed inside the webgpu.hpp file. The webgpu.cppm module file simply defines WEBGPU_CPP_MODULE and includes webgpu.hpp.

This is how the header looks normally:

#include <webgpu.h>
// other includes

namespace wgpu {
// interface
#ifdef WEBGPU_CPP_IMPLEMENTATION
// implementation
#endif
}

And this is how the header looks when the WEBGPU_CPP_MODULE is defined. This is intended to be compiled as a standalone module unit:

module;

#include <webgpu.h>
// other includes

export module webgpu;

export namespace wgpu {
// interface
// implementation
}

@eliemichel
Copy link
Owner

eliemichel commented May 8, 2024

This looks nice! Is there a way to avoid the presence of a .cppm file? Is this a standard extension (not very used to modules yet)? And could you provide a minimal usage example (e.g., just creating a wgpu instance)?

@eliemichel
Copy link
Owner

eliemichel commented May 29, 2024

I finally found the time to test this, and run into issues on MSVC:

error C7579: the module declaration following a global module fragment cannot be produced by macro expansion or header inclusion

I think we'll need to reorganize this a little bit, but the overall idea is good!

Maybe a solution would be to have the webgpu.cppm file look like this:

module;

#define WEBGPU_CPP_MODULE

#define WEBGPU_CPP_GLOBAL_MODULE_FRAGMENT_ONLY
#include "webgpu.hpp"
#undef WEBGPU_CPP_GLOBAL_MODULE_FRAGMENT_ONLY

export module webgpu;

#define WEBGPU_CPP_NO_GLOBAL_MODULE_FRAGMENT
#include "webgpu.hpp"
#undef WEBGPU_CPP_NO_GLOBAL_MODULE_FRAGMENT

edit: Also we should make sure to enclose the #include <webgpu/webgup.h in a export { } block to re-export the raw C API.

@tsarn
Copy link
Author

tsarn commented May 29, 2024

Ah, I didn't know this doesn't work like this on MSVC. I unfortunately don't have time at the moment to look at how to fix that :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants